﻿public partial class Integration : ICollectionChanged
{
    partial void OnResetObservableCollections();
    partial void OnCreateObservableCollections();
    partial void OnUndoObservableCollections();
    partial void OnCreateValidationResults();
    partial void OnSerializingStart();

    protected override void OnLoaded(bool isInitialLoad)
    {
        base.OnLoaded(isInitialLoad);
        OnCreateValidationResults();
        if (!isInitialLoad)
        {
            OnResetObservableCollections();
        }
        OnCreateObservableCollections();
    }

    #region IsChildCollectionChanged Propoerty

    private bool _isChildCollectionChanged;

    public bool IsChildCollectionChanged
    {
        get { return _isChildCollectionChanged; }
        set
        {
            _isChildCollectionChanged = value;
            RaisePropertyChanged("IsChildCollectionChanged");
            RaisePropertyChanged("IsEntityChanged");
        }
    }

    #endregion

    #region IsEntityChanged Propoerty

    public bool IsEntityChanged
    {
        get { return HasChanges || IsChildCollectionChanged; }
        set
        {
            RaisePropertyChanged("IsEntityChanged");
        }
    }
    #endregion

    protected override void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "HasChanges")
            RaisePropertyChanged("IsEntityChanged");
        base.OnPropertyChanged(e);
    }

    public void Undo()
    {
        var rt = this as IRevertibleChangeTracking;
        rt.RejectChanges();
        OnUndoObservableCollections();

    }

    public void MarkDirty()
    {
        IsChildCollectionChanged = true;
    }

    public void ClearDirty()
    {
        IsChildCollectionChanged = false;
        this.AcceptChanges();
    }



    [OnSerializing]
    public void OnSerializing(StreamingContext context)
    {
        OnSerializingStart();
    }
}